package com.MedDroid.task;

import android.os.AsyncTask;
import com.MedDroid.activity.GraphActivity;
import com.MedDroid.control.DatabaseControl;
import com.MedDroid.entities.User;
import com.MedDroid.entities.user.DatedDouble;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Romen
 * Date: 03/12/13
 * Time: 3:43 AM
 * Description:
 */
public class LoadGraphTask extends AsyncTask<Integer, Void, ArrayList<DatedDouble>>
{
	DatabaseControl DatabaseC;
	WeakReference<GraphActivity> GraphPage;

	public LoadGraphTask(DatabaseControl Data, GraphActivity activity)
	{
		DatabaseC = Data;
		GraphPage = new WeakReference<GraphActivity>(activity);
	}

	@Override
	protected ArrayList<DatedDouble> doInBackground(Integer... UserID)
	{
		User Data = DatabaseC.findUser(User.UserIDStr,Integer.toString(UserID[0]));
		if (Data != null)
		{
			return Data.getWeightHistory();
		}
		return null;
	}

	@Override
	protected void onPostExecute(ArrayList<DatedDouble> data)
	{
		GraphActivity Page = GraphPage.get();
		for (int i=0; i<10 && i<data.size(); i++)
		{
			Page.xaxis[i] = (int) data.get(i).getTime();
			Page.yaxis[i] = (int) data.get(i).getData();
		}
		Page.drawGraph();
	}
}
